<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function create(Request $request)
    {
        $options = $request->all();
        $options['user_id'] = $request->user()->id;
        $project = $this->projectService->create($options);
        if(!$project) {
            return $this->failed("Failed to create project");
        }
        return $this->success($project);
    }

    public function get(Request $request, $projectId = null)
    {
        if(!$projectId) {
            return $this->failed("Missing project Id");
        }
        $project = $this->projectService->get($projectId, $request->user()->id);
        if (!$project) {
            return $this->failed("Invalid project id");
        }
        return $this->success($project);
    }

    public function delete(Request $request, $projectId = null)
    {
        if(!$this->projectService->delete($projectId)){
            return $this->failed("删除失败！");
        }
        return $this->success();
    }

    public function getAll(Request $request)
    {
        $limit = $request->get('limit');
        $size = $request->get('size');
        $projects = $this->projectService->getAll($limit, $size);
        return $this->success($projects);
    }
}
